package com.pet.server.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.pet.server.domain.vo.GoodsOrderVO;
import com.pet.server.service.IGoodsOrderService;
import com.pet.server.utils.ResultUtil;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "GoodsOrderServlet", urlPatterns = "/goodsorder")
public class GoodsOrderServlet extends HttpServlet {
  @Resource IGoodsOrderService gos;

  @Override
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }

  @Override
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    String method = request.getParameter("method");

    if (method == null || "".equals(method)) {
      getAllGoodsOrderListByPage(request, response);
    } else if (method.equalsIgnoreCase("queryByGoodsName")) {
      findGoodsOrdersByName(request, response);
    } else if (method.equalsIgnoreCase("queryByStatus")) {
      findGoodsOrdersByStatus(request, response);
    } else if (method.equalsIgnoreCase("queryByPetMaster")) {
      findGoodsOrdersByPetMaster(request, response);
    } else if (method.equalsIgnoreCase("getGoodsOrderInfo")) {
      getGoodsOrderInfoSelective(request, response);
    } else if (method.equalsIgnoreCase("updateGoodsOrder")) {
      updateGoodsOrderSelective(request, response);
    }
  }

  private void findGoodsOrdersByPetMaster(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));
    String petMasterName = request.getParameter("petMasterName");

    List<GoodsOrderVO> voList =
        gos.getGoodsOrdersByPetMaster(pageNo, pageSize, shopId, petMasterName);

    ResultUtil<List> r = null;
    if (voList != null) {
      r = new ResultUtil<>(voList);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void findGoodsOrdersByStatus(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));
    int shopId = Integer.parseInt(request.getParameter("shopId"));
    String status = request.getParameter("status");

    List<GoodsOrderVO> voList = gos.getGoodsOrdersByStatus(pageNo, pageSize, shopId, status);

    ResultUtil<List> r = null;
    if (voList != null) {
      r = new ResultUtil<>(voList);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void updateGoodsOrderSelective(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    GoodsOrderVO orderVO = parseDataFromJson(request, response);

    boolean b = gos.updateGoodsOrderSelective(orderVO);

    ResultUtil<Boolean> r = null;
    if (b) {
      r = new ResultUtil<>(b);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void getGoodsOrderInfoSelective(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int shopId = Integer.parseInt(request.getParameter("shopId"));
    int goodsOrderId = Integer.parseInt(request.getParameter("id"));

    GoodsOrderVO vo = gos.getGoodsOrderSelectiveById(shopId, goodsOrderId);

    ResultUtil<GoodsOrderVO> r = null;
    if (vo != null) {
      r = new ResultUtil<>(vo);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void findGoodsOrdersByName(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    String goodsName = request.getParameter("goodsName");
    int shopId = Integer.parseInt(request.getParameter("shopId"));

    List<GoodsOrderVO> list = gos.getGoodsOrdersByGoodsName(pageNo, pageSize, goodsName, shopId);
    ResultUtil<List> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);
    } else {
      r = new ResultUtil<>("无数据");
    }
    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  private void getAllGoodsOrderListByPage(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    int pageNo = Integer.parseInt(request.getParameter("pageNo"));
    int pageSize = Integer.parseInt(request.getParameter("pageSize"));

    int shopId = Integer.parseInt(request.getParameter("shopId"));

    List<GoodsOrderVO> list = gos.getAllGoodsOrders(pageNo, pageSize, shopId);

    ResultUtil<List> r = null;
    if (list != null) {
      r = new ResultUtil<>(list);
    } else {
      r = new ResultUtil<>("无数据");
    }

    response.getWriter().write(new ObjectMapper().writeValueAsString(r));
  }

  /**
   * 通过Jackson的mapper方法 ,生成对象
   *
   * @param request req
   * @param response resp
   * @return 商品对象数据
   */
  private GoodsOrderVO parseDataFromJson(HttpServletRequest request, HttpServletResponse response) {
    // 声明传入数据类型
    response.setContentType("application/json");

    GoodsOrderVO vo = null;

    // 获取reader
    try (BufferedReader reader = request.getReader()) {
      // 读取json数据流
      String json = reader.readLine();

      System.out.println("json = " + json);
      // 通过Jackson的Mapper方法反序列化,生成对象

      vo = new ObjectMapper().readValue(json, GoodsOrderVO.class);

    } catch (IOException e) {
      e.printStackTrace();
    }
    return vo;
  }
}
